import { Object3D, Sprite, SpriteMaterial, Vector3 } from "three";
import { App, Entity, EventModule, Tween, generateUUID, getBoundingBox } from "vjmap3d";

// 抬杆相关方法

/** 创建一个动画点精灵 */
export const createWavePoint = (parent: Object3D, position: Vector3, app: App) => {
    let spriteMaterial = new SpriteMaterial({
        map: app.getLoadedResources().getTexture("wave"), 
        transparent: true,
        color:0x00ffff,
    });
    let sprite = new Sprite(spriteMaterial);
    sprite.position.copy(position);
    parent.add(sprite);

    // 和创建的对象相关联，创建一个实体，用来做动画
    let spriteEntity = Entity.attchObject(sprite, false, false, app);
    let s = 0.0;
    let maxScale = 0.6;
    spriteEntity.addAction(() => {
        s += 0.01;
        if (s < 0.5) {
            sprite.scale.x = maxScale * (1 + s);
            sprite.scale.y = maxScale * (1 + s);
        } else if (s >= 0.5 && s < 1.0) {
            sprite.scale.x = maxScale * (2 - s);
            sprite.scale.y = maxScale * (2 - s);
        } else {
            s = 0.0;
        }
    })
    return {
        sprite,
        spriteEntity
    }
}

/** 创建抬杆 */
export const createPoles = (app: App) => {
    let poles = [];
    let resources = app.getLoadedResources();
    let tollModel = resources.getModel("toll");
    // 给每个抬杆添加一个模型热点
    for (let i = 0; i < 8; i++) {
        const gz = tollModel.getObjectByName('GZ00' + (i + 1));
        let box3d = getBoundingBox(gz);
        const size = new Vector3();
        box3d.getSize(size);
        let position = new Vector3(0, 0, 0)
        position.x += size.x * 0.7; // 根据包围盒尺寸移动热点模型
        position.z += 0.5; //在档杆前方适当偏移，避免旋转的时候被挡杆遮挡部分：
        let hotPoint = createWavePoint(gz, position, app);
        // 动画
        let anmi = createPoleAnimate(gz);

        let isOpened = false;// 抬杆是否抬起
        // 抬起抬杆
        const openPole = () => {
            isOpened = true;
            anmi.open.start(); // 开启抬起动画
        }
         // 降下抬杆
         const closePole = () => {
            isOpened = false;
            anmi.close.start(); // 开启降下动画
        }

        // 点击抬杆上模型热点时自动抬杆或降下
        // hotPoint.spriteEntity.pointerEvents = true; // 设置可接收鼠标事件
        // hotPoint.spriteEntity.signal.onMouseClick.add(() => {
        //     if (isOpened) closePole();
        //     else openPole()
        // })
        hotPoint.spriteEntity.add(EventModule, {
            clickHighlight: false,
            hoverSelect: true,
            // @ts-ignore
            clickCallback: (entity: Entity, isClick: boolean) => {
              if (!isClick) return; // 点击了其他实体
              if (isOpened) closePole();
              else openPole()
            }
        })

        let obj = {
            pole: gz,
            ...hotPoint,
            openPole,
            closePole
        };
        poles.push(obj)
    }
    return poles;
}

/** 创建抬杆动画 */
export const createPoleAnimate = (pole: Object3D) => {
    const animId = generateUUID(); // 两个动画用同一个id，这样启动另外一个动画时，会把同一个id的动画先停止
    const open = new Tween(pole.rotation).setId(animId).to(2000, { z: Math.PI / 2 }, { easing: "linear" });
    const close = new Tween(pole.rotation).setId(animId).to(2000, { z: 0 }, { easing: "linear" });
    return {
        open, 
        close
    }
}